from django import forms
from web01 import models


class Articleform(forms.ModelForm):
    class Meta:
        model = models.Article
        fields = '__all__'
        exclude = ['count_read', 'content', 'publish_time']

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        user_list = []
        for i in models.UserInfo.objects.all():
            user_list.append((i.pk, i.name))
        self.fields['author'].choices = user_list
        category_list = []
        for c in models.Category.objects.all():
            if c.subcategory:
                category_list.append((c.pk, c.bigcategory.title + '|' + c.subcategory.title))
        self.fields['category'].choices = category_list


class ArticleDetailForm(forms.ModelForm):
    class Meta:
        model = models.ArticleDetail
        fields = "__all__"
